#! /usr/bin/env python
#coding=utf-8

import pickle
import strings

from hsbasic import HSBasic

"""

    Class which manages local highscores i.e.
    save score table on disk, load score table from disk, etc.

"""

class HSLocal(HSBasic):

    SCORE_FILENAME = "highscores"
    SCORES_COUNT = 25
    
    def __init__(self):
        HSBasic.__init__(self)        

    def getRawData(self):
        data = []
        try:
            f = open(HSLocal.SCORE_FILENAME, 'rb')
            data = f.read()
            f.close()
        except IOError:
            # error reading file, try to create a new empty
            # high scores data file
            self.addNewScoreTable(strings.STR_MAIN_MENU_STANDART, capacity = HSLocal.SCORES_COUNT)
            self.addNewScoreTable(strings.STR_MAIN_MENU_SURVIVE, capacity = HSLocal.SCORES_COUNT)
            stream = self.dumpScoresData()
            f = open(HSLocal.SCORE_FILENAME, 'wb')
            f.write(stream.getvalue())
            f.close()                
        return data

    def saveRawData(self):
        data = self.dumpScoresData()
        f = open(HSLocal.SCORE_FILENAME, 'wb')
        f.write(data.getvalue())
        f.close()